package com.ryl.yomall.order.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Date 11:24
 * @Author ryl
 */
@Configuration
public class DeadLetterMQConfig {
    public static final String EXCHANGE_DIRECT_ORDER_CANCEL ="exchange.direct.order.cancel";
    public static final String ROUTING_ORDER_CANCEL1 = "order.create1";
    public static final String QUEUE_ORDER_CANCEL1  = "queue.order.cancel1";
    public static final String ROUTING_ORDER_CANCEL2 = "order.create2";
    public static final String QUEUE_ORDER_CANCEL2  = "queue.order.cancel2";
    //取消订单 延迟时间 单位：毫秒
    public static final int DELAY_TIME  = 2*60*60*1000;
    @Bean
    public Queue queue1(){
        Map<String, Object> arguments=new HashMap<> (  );
        arguments.put ( "x-dead-letter-exchange", EXCHANGE_DIRECT_ORDER_CANCEL);
        arguments.put ( "x-dead-letter-routing-key", ROUTING_ORDER_CANCEL2);
        arguments.put("x-message-ttl",DELAY_TIME);//单位是毫秒
        return QueueBuilder.durable ( QUEUE_ORDER_CANCEL1 ).withArguments ( arguments ).build ();
    }
    @Bean
    public Queue queue2(){
        return QueueBuilder.durable ( QUEUE_ORDER_CANCEL2 ).build ();
    }
    @Bean
    public Exchange exchange(){
        return ExchangeBuilder.directExchange ( EXCHANGE_DIRECT_ORDER_CANCEL ).build ();
    }
    @Bean
    public Binding binding1(@Qualifier("queue1") Queue queue1,@Qualifier("exchange") Exchange exchange){
        return BindingBuilder.bind ( queue1 ).to ( exchange ).with ( ROUTING_ORDER_CANCEL1 ).noargs ();
    }
    @Bean
    public Binding binding2(@Qualifier("queue2") Queue queue2,@Qualifier("exchange") Exchange exchange){
        return BindingBuilder.bind ( queue2 ).to ( exchange ).with ( ROUTING_ORDER_CANCEL2 ).noargs ();
    }

}
